﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using DTcms.DBUtility;
using DTcms.Common;

namespace DTcms.DAL
{
    /// <summary>
    /// 数据访问类:商品价格
    /// </summary>
    public partial class article_goods
    {
        private string databaseprefix;//数据库表名前缀
        public article_goods(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }

        /// <summary>
        /// 根据规格列表查询商品实体
        /// </summary>
        public Entitys.dt_article_goods GetModel(int channel_id, int article_id, string spec_ids)
        {

            StringBuilder strSql = new StringBuilder();
            StringBuilder str1 = new StringBuilder();
            Entitys.dt_article_goods model = new Entitys.dt_article_goods();
            //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            foreach (PropertyInfo p in pros)
            {
                //拼接字段，忽略List<T>
                if (!p.Name.Equals("group_prices"))
                {
                    str1.Append(p.Name + ",");
                }
            }
            strSql.Append("select top 1 " + str1.ToString().Trim(','));
            strSql.Append(" from " + databaseprefix + "article_goods");
            strSql.Append(" where channel_id=@channel_id and article_id=@article_id and spec_ids=@spec_ids");
            SqlParameter[] parameters = {
					new SqlParameter("@channel_id", SqlDbType.Int,4),
                    new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@spec_ids", SqlDbType.NVarChar,500)};
            parameters[0].Value = channel_id;
            parameters[1].Value = article_id;
            parameters[2].Value = spec_ids;
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

            if (dt.Rows.Count > 0)
            {
                return DataRowToModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }
        }

    }
}